/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Bill.java
 *
 * Created on 25-Apr-2011, 2:38:44 PM
 */

package pos.ui.screen;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import pos.control.OrderControl;
import pos.control.PurchasedItemControl;
import pos.desktop.controller.PurchaseController;
import pos.entity.Order;
import pos.entity.PurchasedItem;

/**
 * This the boundary object to display the Bill details
 * @author rsrikanth
 */
public class Bill extends javax.swing.JFrame {
    
    /** Creates new form Bill */
    public Bill(Order order) {
        purchasedItemList = new ArrayList<PurchasedItem>();
        this.order = order;
        initComponents();
        setTitle("Bill");
        setSize(550,580);
    }
    
    ArrayList<PurchasedItem> purchasedItemList = null;
    Order order = null;
    /**
     * Sets the purchased items for displaying the details on the screen
     * @param purchasedItemList 
     */
    public void setPurchasedItemList(ArrayList<PurchasedItem> purchasedItemList){
        this.purchasedItemList = purchasedItemList;
        PurchasedItemControl pic = new PurchasedItemControl();
        pic.updatePurchasedItemPriceAttributes(purchasedItemList);
        
        OrderControl oc = new OrderControl();
        order = oc.updateOrderTotalPrices(null, purchasedItemList);
        purchasedItemList = order.getPurchasedItems();
        populateBill(order);
    }
    
    /**
     * Populates the bill
     * @param order 
     */
    public void populateBill(Order order){
        ArrayList<PurchasedItem> purchasedList = order.getPurchasedItems();
        StringBuffer sb = new StringBuffer();
        sb.append("DigiShop Bill\n");
       sb.append("----------\n");
        for(int i=0; i<purchasedList.size(); i++){
           PurchasedItem pi = purchasedList.get(i);
           sb.append(pi.getName());
           sb.append(" ");
           appendCurrency(sb, pi.getTotalPrice());
           sb.append(" ");
           appendCurrency(sb, pi.getPriceAdjustedAmount());
           sb.append("\n");
       } 
       sb.append("----------\n");
       sb.append("Tax ");
       appendCurrency(sb, order.getOrderTotalTaxes());
       sb.append("\n");
       sb.append("Net Total ");
       appendCurrency(sb, order.getOrderTotalPrice());
       
       sb.append("\nThank you!!");
       
       billTA.setText(sb.toString());
    }
    
    private StringBuffer appendCurrency(StringBuffer sb, Object value){
        sb.append("$" + value +"0");
        return sb;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        okBtn = new javax.swing.JButton();
        saveBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        billTA = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        okBtn.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        okBtn.setText("OK");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okBtnActionPerformed(evt);
            }
        });

        saveBtn.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        saveBtn.setText("Save");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addComponent(okBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(19, 453, 480, 90);

        billTA.setColumns(20);
        billTA.setRows(5);
        jScrollPane1.setViewportView(billTA);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 50, 470, 360);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Bill");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(230, 10, 30, 22);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
        this.dispose();
    }//GEN-LAST:event_okBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
try{
    // Create file 
    FileWriter fw = new FileWriter(order.getOrderId()+"-bill.txt");
    BufferedWriter out = new BufferedWriter(fw);
    out.write(billTA.getText());
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error while writing file " + e.getMessage());
    }        
    }//GEN-LAST:event_saveBtnActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea billTA;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okBtn;
    private javax.swing.JButton saveBtn;
    // End of variables declaration//GEN-END:variables

}
